package me.xiaoyan.java.spring.dao;

import me.xiaoyan.java.spring.mapper.LinkMapper;
import me.xiaoyan.java.spring.po.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class LinkDaoImpl implements LinkDao{
    public LinkMapper linkMapper;

    @Autowired
    public void setLinkMapper(LinkMapper linkMapper) {
        this.linkMapper = linkMapper;
    }

    @Override
    public void save(Link link) {
        // 只要id大于0 说明是更新数据
        if(link.getId() > 0){
            Link l = findById(link.getId()); // 先根据id查询一次
            if(l != null){
                linkMapper.update(link);
                return;
            }
        } // 没有找到对应数据 即进行新增操作
        linkMapper.insert(link);
    }

    @Override
    public List<Link> findList(int count) {
        return linkMapper.select(count);
    }

    @Override
    public Link findById(Integer id) {
        return linkMapper.selectById(id);
    }
}
